/*
 * TreeNode.java
 *
 * Created on 06 April 2003, 07:26
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import java.util.*;
import javax.ejb.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.NodeValue;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

/**
 *
 * @author  Robert Crida Work
 */
public class GuiTreeNode {
    
    private KeySession m_key;
    private NodeValue m_nodeValue;
    
    /** Creates a new instance of TreeNode */
    public GuiTreeNode(KeySession p_key, NodeValue p_nodeValue) {
        m_key = p_key;
        m_nodeValue = p_nodeValue;
    }
    
    /** Generate a suitable display panel for the current node by checking its
     * type and then instantiating the appropriate gui class. */
    public java.awt.Component getDisplay() {
        return NodeDisplay.getDisplay(m_key, getNodeValue(), getPermissions());
    }
    
    public NodeValue getNodeValue() {
        return m_nodeValue;
    }
    
    public void setNodeValue(NodeValue p_nodeValue) {
        m_nodeValue = p_nodeValue;
    }
    
    public void refreshNodeValue() {
        try {
            m_nodeValue = StatelessRemoteClientSessionUtil.getSession().getNodeValue(m_key, m_nodeValue.getPrimaryKey());
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(null, "Permission Denied!", "Refresh node", JOptionPane.WARNING_MESSAGE);
        }
    }

    public Set getPermissions() {
        try {
            return StatelessRemoteClientSessionUtil.getSession().getAssignedPermissions(m_key, m_nodeValue.getPrimaryKey());
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        return null;
    }

    public Collection getChildrenNodeValue() {
        try {
            return StatelessRemoteClientSessionUtil.getSession().getVisibleChildrenValues(m_key, m_nodeValue.getPrimaryKey());
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        return null;
    }

    /** Converts the node or leaf object to a string for representation in
     * the tree */
    public String toString() {
        return m_nodeValue.getName();
    }

    public String getType() {
        return m_nodeValue.getFinalType();
    }
    
    public boolean isActive() {
        try {
            if (m_nodeValue.getType().equals("Task")) {
                return StatelessRemoteClientSessionUtil.getSession().isTaskActive(m_key, new TaskPK(m_nodeValue.getNodeId()));
            }
            if (m_nodeValue.getType().equals("User")) {
                return StatelessRemoteClientSessionUtil.getSession().isUserEnabled(m_key, new UserPK(m_nodeValue.getNodeId()));
            }
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        return false;
    }
}
